package my.jieh.tips.xml;

import java.io.*;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

public class XMLDom4j {
  public XMLDom4j() {
  }

  public void generateDocument(){
    Document document = DocumentHelper.createDocument();

    Element catalogElement = document.addElement("catalog");
    catalogElement.addComment("An XML Catalog");
    catalogElement.addProcessingInstruction("target", "text");

    Element journalElement = catalogElement.addElement("journal");
    journalElement.addAttribute("title", "XML Zone");
    journalElement.addAttribute("publisher", "IBM developerWorks");

    Element articleElement = journalElement.addElement("article");
    articleElement.addAttribute("level", "Intermediate");
    articleElement.addAttribute("date", "December-2001");

    Element titleElement = articleElement.addElement("title");
    titleElement.setText("Java configuration with XML Schema");

    Element authorElement = articleElement.addElement("author");
    Element firstNameElement = authorElement.addElement("firstname");
    firstNameElement.setText("jieh");
    Element lastNameElement = authorElement.addElement("lastname");
    lastNameElement.setText("lin");

//    document.addDocType("catalog", null, "file://c:/Dtds/catalog.dtd");

    try {
      XMLWriter out = new XMLWriter(new FileWriter(new File("./catalog.xml")));
      out.write(document);
      out.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }

  }

  public static void main(String[] args) {
    XMLDom4j xmldom4j = new XMLDom4j();
    xmldom4j.generateDocument();
  }
}
